package com.zs.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zs.blog.constant.enums.SQLConstant;
import com.zs.blog.converter.RoleConverter;
import com.zs.blog.converter.UserConverter;
import com.zs.blog.dao.IRoleMapper;
import com.zs.blog.dao.IUserMapper;
import com.zs.blog.dao.IUserRoleMapper;
import com.zs.blog.model.dto.RoleListDTO;
import com.zs.blog.model.dto.UserListDTO;
import com.zs.blog.model.entity.User;
import com.zs.blog.model.entity.UserRole;
import com.zs.blog.model.vo.PageInfoVO;
import com.zs.blog.model.vo.RoleListVO;
import com.zs.blog.model.vo.UserListVO;
import com.zs.blog.service.IUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Title: IUserRoleServiceImpl
 * @Author zs
 * @Package com.zs.blog.service.impl
 * @Date 2025/5/18 20:27
 * @description: 用户-角色服务层实现类
 */
@Service
public class IUserRoleServiceImpl extends ServiceImpl<IUserRoleMapper, UserRole> implements IUserRoleService {

    public IUserRoleMapper userRoleMapper;

    public IUserMapper userMapper;

    public IRoleMapper roleMapper;

    @Autowired
    public IUserRoleServiceImpl(IUserRoleMapper userRoleMapper, IUserMapper userMapper, IRoleMapper roleMapper) {
        this.userRoleMapper = userRoleMapper;
        this.userMapper = userMapper;
        this.roleMapper = roleMapper;
    }

    /**
     * 根据角色ID获取用户列表 分页
     *
     * @param roleId   角色ID
     * @param pageNum  页码
     * @param pageSize 每页数据量
     * @return 分页的用户列表信息
     */
    @Override
    public PageInfoVO<UserListVO> listUserByRoleId(Integer roleId, Integer pageNum, Integer pageSize) {

        IPage<UserListDTO> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<UserRole> lqw = new LambdaQueryWrapper<>();
        /*
          这里的lqw操作的是user_role_rel这个表，但是联查时需要查询um_user表中is_deleted为0的数据
          所以使用mp提供的子查询方法exists()
          生成的SQL例子: SELECT * FROM user WHERE EXISTS (select id from table where age = 1)

          因为这里是自己写的SQL也可以直接在SQL语句中写 where子句
         */
        //todo
        lqw.eq(UserRole::getRoleId, roleId).exists(SQLConstant.MP_EXISTS_SELECT_UM_USER_IS_DELETED);
        List<UserListDTO> userListDTOS = userRoleMapper.listUserByRoleId(page, lqw);

        List<UserListVO> userListVOS = new ArrayList<>();
        userListDTOS.forEach(item -> {
            userListVOS.add(UserConverter.INSTANCE.userListDTO2UserListVO(item));
        });

        PageInfoVO<UserListVO> pageInfo = new PageInfoVO<>();
        pageInfo.setTotal(page.getTotal());
        pageInfo.setItems(userListVOS);

        return pageInfo;
    }

    /**
     * 根据用户ID获取未分配角色列表
     *
     * @param userId     用户ID
     * @param pageNum    页码
     * @param pageSize   每页数据量
     * @param roleIdList 角色ID列表
     * @return 分页的用户列表信息
     */
    @Override
    public PageInfoVO<RoleListVO> listRoleByUserId(Long userId, Integer pageNum, Integer pageSize, List<Long> roleIdList) {
        IPage<RoleListDTO> page = new Page<>(pageNum, pageSize);

        LambdaQueryWrapper<UserRole> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UserRole::getUserId,userId)
                // 用户未分配的角色
                .in(UserRole::getRoleId,roleIdList)
                // 去除已删除和已禁用的记录
                // todo
                .exists(SQLConstant.MP_EXISTS_SELECT_UM_USER_IS_DELETED_IS_DISABLED);

        List<RoleListDTO> roleListDTOS = userRoleMapper.listRoleByUserId(page,lqw);

        List<RoleListVO> roleListVOS = new ArrayList<>();
        roleListDTOS.forEach(item->{
            roleListVOS.add(RoleConverter.INSTANCE.roleListDTO2RoleListVO(item));
        });

        PageInfoVO<RoleListVO> pageInfo = new PageInfoVO<>();
        pageInfo.setTotal(page.getTotal());
        pageInfo.setItems(roleListVOS);
        return pageInfo;
    }
}
